﻿using StationNF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLCConsole
{
    public enum StationPLCType
    {
        None = 0,
        ModbusPLC = 1,
        WFDRVX = 2,
        ADP2203VX = 3,
        IOPLCMVX = 4,
        NET2RSVX = 5,
        ICDEXPVX = 6,
        STM32AD76VX = 7,
    }

    public abstract class Station_PLCBase
    {
        public string StationID { get; protected set; } = "";
        protected IFStation tSourStation
        {
            get
            {
                if ((tCurrentStation != null) && (!string.IsNullOrEmpty(tCurrentStation.StaID)))
                {
                    StationID = tCurrentStation.StaID;
                    return tCurrentStation;
                }
                if ((StationServer != null) && (!string.IsNullOrEmpty(StationID)))
                {
                    tCurrentStation = StationServer.getStation(StationID);
                    return tCurrentStation;
                }
                return null;
            }
        }
        protected IFStation tCurrentStation { get; private set; } = null;
        protected IFStationsServer StationServer { get; private set; } = null;
        public StationPLCType StationType { get; protected set; } = StationPLCType.None;

        public bool HasFlashRegs
        {
            get
            {
                if (isReady)
                    return tSourStation.hasFlash;
                return false;
            }
        }

        public string StaID
        {
            get
            {
                if (isReady)
                    return tSourStation.StaID;
                return "";
            }
        }

        /// <summary>
        /// 检测工作站数据对象是否正常
        /// </summary>
        public virtual bool isReady { get { return tSourStation != null; } } 

        public DateTime LastActiveTime
        {
            get
            {
                if (isReady)
                    return tSourStation.LastActiveTime;
                return DateTime.MinValue;
            }
        }

        public Station_PLCBase(IFStation SourStation)
        {
            tCurrentStation = SourStation;
            if (tSourStation != null)
                StationID = tSourStation.StaID;
        }

        public Station_PLCBase(IFStationsServer SourStaServer,string DestStaID)
        {
            StationServer= SourStaServer;
            StationID = DestStaID;
        }
    }
}
